package cn.coldleaf.wiki.pojo.vo;

import cn.coldleaf.wiki.utils.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 接口响应结果
 * @datetime 2023/10/15 下午1:36
 * @user cold
 **/
@Data
@ApiModel(value = "ResponseVo(接口响应结果根VO)",description = "接口响应结果根VO")
public class ResponseVo<T> {
    /**
     * 结果代码:
     * 0 - 操作成功
     * 99- 操作异常
     **/
    @ApiModelProperty("结果代码")
    private int code;

    /**
     * 结果消息
     **/
    @ApiModelProperty("结果消息")
    private String msg;

    /**
     * 返回数据
     **/
    @ApiModelProperty("结果数据")
    private T data;

    /**
     * 请求标识
     **/
    @ApiModelProperty("请求标识")
    private String requestId;

    /**
     * 请求时间
     **/
    @ApiModelProperty("请求时间戳")
    private long requestTime;

    /**
     * 响应时间
     **/
    @ApiModelProperty("响应时间戳")
    private long timestamp;

    private ResponseVo(){}

    private ResponseVo(int code,String msg,T data){
        this.code = code;
        this.msg = msg;
        this.timestamp = System.currentTimeMillis();
        this.data = data;
    }

    private ResponseVo(ErrorCode errorCode, T data){
        this(errorCode.getCode(),errorCode.getMsg(),data);
    }

    public static <T>ResponseVo<T> success(T data){
        return new ResponseVo<T>(ErrorCode.SUCCESS,data);
    }

    public static <T>ResponseVo<T> exception(){
        return new ResponseVo<T>(ErrorCode.EXCEPTION,null);
    }

    public static <T>ResponseVo<T> error(ErrorCode errorCode){
        return new ResponseVo<T>(errorCode,null);
    }

    public static <T>ResponseVo<T> error(int code,String msg,T data){
        return new ResponseVo<T>(code,msg,data);
    }
}
